package tests;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import utils.Config;

import database.Reader;
import database.Update;
import database.UserManager;
import database.Writer;

public class DBTests {

	private Reader reader;
	private Writer writer;
	private UserManager userManager;
	private Config config;
	private Update up;

	@Before
	public void setup() {
		reader = new Reader();
		writer = new Writer();
		config = Config.getInstance();
		up = new Update();

	}

	@Test
	public void testSetChangeUserName() {
		String usernameBefore = config.getDBUserName();
		config.setDBUserName(usernameBefore + "1");
		String usernameLater = config.getDBUserName();
		Assert.assertNotSame(usernameBefore, usernameLater);
		Assert.assertTrue(usernameLater.equals(usernameBefore + "1"));
		config.setDBUserName(usernameBefore);
	}

	@Test
	public void testWriteSponsor() throws SQLException, ClassNotFoundException {

		List<String> sponsors = reader.getSponsorsName();
		if (!sponsors.contains("test company 12345")) {
			writer.writeSponsor("test company 12345", 1234.0, "gold");
		}
		sponsors = reader.getSponsorsName();
		Assert.assertTrue(sponsors.contains("test company 12345"));

	}

	@Test
	public void testWriteNamePath() throws SQLException, ClassNotFoundException {
		List<String> sponsors = reader.getSponsorsName();
		if (!sponsors.contains("test company 12345")) {
			writer.writeSponsor("test company 12345", 1234.0, "gold");
		}
		writer.writeNamePath("test company 12345", "thisIsTheVideoPath");
		List<String> paths = reader.getPathOwnedBySponsor("test company 12345");
		Assert.assertTrue(paths.contains("thisIsTheVideoPath"));
	}

	@Test
	public void testUpdateRemove() throws SQLException, ClassNotFoundException {
		List<String> sponsors = reader.getSponsorsName();
		Assert.assertTrue(sponsors.contains("test company 12345"));
		up.removeSponsorFromBD("test company 12345");
		sponsors = reader.getSponsorsName();
		Assert.assertFalse(sponsors.contains("test company 12345"));
	}

	@Test
	public void testWriteRss() {
		List<String> feedsAvailable = new ArrayList<String>();
		if (!feedsAvailable.contains(("weather"))) {
			writer.writeRss("urlTest", "weather", "10/10/09");
		}
		feedsAvailable = reader.getFeedsAvailable();
		Assert.assertTrue(feedsAvailable.contains("weather"));

	}

	@Test
	public void testRssRemove() throws SQLException, ClassNotFoundException {
		List<String> feedsAvailable = reader.getFeedsAvailable();
		Assert.assertTrue(feedsAvailable.contains("weather"));
		up.removeFeedFromDB("weather");
		feedsAvailable = reader.getFeedsAvailable();
		Assert.assertFalse(feedsAvailable.contains("weather"));
	}

}